package com.api.oa.services.impl;

import com.api.oa.config.EmServer;
import com.api.oa.mapper.OaMessageMapper;
import com.api.oa.mapper.OrgMapper;
import com.api.oa.model.entity.OaMessage;
import com.api.oa.model.entity.OaOrg;
import com.api.oa.model.vo.OaMessageVO;
import com.api.oa.services.IOaMessageService;
import com.api.shopapi.config.WebSocket;
import com.easemob.im.server.EMService;
import com.easemob.im.server.model.EMKeyValue;
import com.easemob.im.server.model.EMMessage;
import com.easemob.im.server.model.EMTextMessage;
import com.gexin.fastjson.JSON;
import com.gexin.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Service
public class OaMessageServiceImpl implements IOaMessageService {


    @Autowired
    private OaMessageMapper oaMessageMapper;

    @Autowired
    private OrgMapper orgMapper;

    @Autowired
    WebSocket webSocket;

    @Autowired
    EMService service;

    @Override
    public OaMessageVO add(OaMessage oaOrg) throws Exception {
//        OaOrg oneByUserId = orgMapper.findOneByUserId(oaOrg.getMessageTo());
//        if(oneByUserId == null){
//            throw new Exception("未设置负责人,不能通信");
//        }

        oaOrg.setMessageAt(new Date());
        oaMessageMapper.insert(oaOrg);
//        Set<EMKeyValue> set = new HashSet<>();
//        set.add(EMKeyValue.of("type", "web"));
//        Set<String> message = new HashSet<>();
//        message.add(oaOrg.getContent());
//        EMMessage emTextMessage = new EMMessage();
//        service.message().send(oaOrg.getMessageFrom(), "", message, emTextMessage, set);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("type",14);
//        jsonObject.put("data",oaOrg);
//        webSocket.sendInfoToOne(oaOrg.getMessageTo(), jsonObject.toJSONString());
        return oaMessageMapper.findOne(oaOrg.getMessageId());
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public Integer update(OaMessage org) {
        return null;
    }

    @Override
    public List<OaMessageVO> list(Long pid) {
        return null;
    }

    @Override
    public OaOrg one(Long id) {
        return null;
    }

    @Override
    public PageInfo<OaMessageVO> list(String from, String to, Integer pageIndex, Integer pageSize) throws Exception {
//        OaOrg oneByUserId1 = orgMapper.findOneByUserId(from);
//        OaOrg oneByUserId = orgMapper.findNearOne(oneByUserId1.getOrgId(),oneByUserId1.getOrgPid(),to);
//        if(oneByUserId == null){
//            throw new Exception("不可跨机构通信");
//        }
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("messageAt desc,messageId desc");
        PageInfo<OaMessageVO> pageInfo = new PageInfo<>(oaMessageMapper.list(from, to));

        return pageInfo;
    }

    @Override
    public Integer sendVideo(String from, String to) {
        OaOrg oneByUserId = orgMapper.findOneByUserId(from);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", 15);
        JSONObject data = new JSONObject();
        data.put("fromOrgName", oneByUserId.getOrgName());
        data.put("channel", from);
        jsonObject.put("data", data);
        boolean sessionById = webSocket.isExit(to);
        if (!sessionById) {
            return -1; //不在线
        }
        webSocket.sendInfoToOne(to, jsonObject.toJSONString());

        return 1;// 发送成功
    }
}
